using System;

using TrueCode.Core.Aspects;

namespace EMan.BL.EntityEditing
{
  public class EntityProperty< T > : EntityProperty
  {
    private readonly Func< T > _getValue;
    private readonly Action< T > _setValue;

    public EntityProperty( [NotNull] string description, [NotNull] Func< T > getValue )
      : this( description, getValue, null ){}
    public EntityProperty( [NotNull] string description, [NotNull] Func< T > getValue,
                           [CanBeNull] Action< T > setValue ) : base( description )
    {
      if( getValue == null ) throw new ArgumentNullException( "getValue" );
      
      _setValue = setValue;
      _getValue = getValue;
    }

    private T TypedValue
    {
      get{ return _getValue(); }
      set{ _setValue( value ); }
    }

    public override bool IsReadOnly
    {
      get{ return _setValue == null; }
    }

    public override object Value { get{ return TypedValue; } set{ TypedValue = (T)value; } }
  }
}